package com.lianyi.push.share.channelinitializer;

import com.lianyi.push.share.listener.TcpClientToSeiviceMessageListener;
import com.lianyi.push.share.tcpservice.TcpServerHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

/**
 * Created by leilei on 2018/8/17.
 */

public class TcpServiceChannelInitializer extends ChannelInitializer<SocketChannel> {
    private TcpClientToSeiviceMessageListener tcpClientToSeiviceMessageListener;

    public TcpServiceChannelInitializer(TcpClientToSeiviceMessageListener listener) {
        this.tcpClientToSeiviceMessageListener = listener;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
//        ch.pipeline().addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
        ch.pipeline().addLast(new ObjectDecoder(1024 * 1024 * 1024,
                ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
        ch.pipeline().addLast(new ObjectEncoder());
        ch.pipeline().addLast(new TcpServerHandler(tcpClientToSeiviceMessageListener));
    }
}
